package ncov.dao.impl;

import ncov.dao.ReportRecordDao;
import ncov.model.ReportRecord;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import java.util.List;

/**
 * @author WuChangJian
 * @since 2020/5/31 8:43
 */
@Repository
public class ReportRecordDaoImpl implements ReportRecordDao {

    @Autowired
    private SessionFactory sessionFactory;
    private Session getSession() {
        return sessionFactory.getCurrentSession();
    }

    @Override
    public ReportRecord queryById(String stuId) {
        return null;
    }

    @Override
    public List<ReportRecord> queryAllByLimit(int offset, int limit) {
        return null;
    }

    @Override
    public List<ReportRecord> queryAll(ReportRecord reportRecord) {
        Criteria criteria = getSession().createCriteria(ReportRecord.class);
        if (reportRecord.getDepartment() != null)
            criteria.add(Restrictions.eq("department",reportRecord.getDepartment()));
        criteria.add(Restrictions.eq("inClass",reportRecord.getInClass()));
        return criteria.list();
    }

    @Override
    public int count(String deptName) {
        Session session = getSession();
        Query query = session.createQuery("select count(*) from ReportRecord where department=? and currentTemperature > 37.5");
        query.setParameter(0, deptName);
        return ((Long)query.uniqueResult()).intValue();
    }

    @Override
    public int insert(ReportRecord reportRecord) {
        Session session = getSession();
        Serializable save = session.save(reportRecord);
        return (int) save;
    }

    @Override
    public int update(ReportRecord reportRecord) {
        Session session = getSession();
        session.update(reportRecord);
        session.flush();
        return 0;
    }

    @Override
    public int deleteById(Integer id) {
        return 0;
    }
}
